/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: Entity.java 45 2008-05-14 23:04:29Z jens464 $
*/
package albareth.core.entity;


import albareth.core.Game;
import albareth.core.GameEffect;
import albareth.core.GameMap;
import albareth.frontend.Tile;
import albareth.util.Location;
import frosch.util.glider.Easing;

public abstract class Entity extends AbstractRepresentable implements Cloneable
{
    private Location _location = null;

    //private HashMap _properties = null;//new HashMap();
    
    public Entity(String name, Tile tile)
    {
        super(name, tile);
    }
    
    public Object clone() throws CloneNotSupportedException
    {
        Entity clone = (Entity)super.clone();
        if (_location != null) {
            clone.setLocation(new Location(_location));
        }
        return clone; 
    }

    public Location getLocation()
    {
        return _location;
    }
    
    public abstract boolean isMobile();
    
    public boolean isItem()
    {
        return !isMobile();
    }

    public int getX()
    {
        return _location.x;
    }

    public int getY()
    {
        return _location.y;
    }
    
    public int takeDamage(int rawDamage, int damageType, Entity causer)
    {
        return 0;
    }
    
    public int getDistance(Entity other)
    {
        return getLocation().distance(other.getLocation());
    }
    
    public GameMap getMap()
    {
        return _location.map;
    }

    public final void setLocation(GameMap map, int x, int y)
    {
        moveTo(map, x, y);
    }
    public final void setLocation(Location loc)
    {
        moveTo(loc.map, loc.x, loc.y);
    }
    
    public final void moveTo(Location loc)
    {
        moveTo(loc.map, loc.x, loc.y);
    }

    public void moveTo(GameMap map, int x, int y)
    {
        map.moveEntityTo(this, x, y);
    }

    /**
     * Only called by GameMap. Do not call directly. Use moveTo instead.
     */
    public void setLocationInternal(int x, int y, GameMap map)
    {
        _location = new Location(x, y, map);
    }

    public void speak(String spokenText)
    {
        Game.getContext().getPerceptionDispatcher().dispatchSpeech(this, spokenText);
    }
    
    public void broadcast(String message)
    {
        Game.getContext().getPerceptionDispatcher().dispatchBroadcast(this, message);
    }
    
    public void emitEffect(GameEffect effect)
    {
        effect.setEmitter(this);
        Game.getContext().getPerceptionDispatcher().dispatchEffect(this, effect);
    }

    public void emitSound(String soundId)
    {
        Game.getContext().getPerceptionDispatcher().dispatchSound(this, soundId);
    }

    public void emitOverheadMessage(String message, String color, Easing easing)
    {
        Game.getContext().getPerceptionDispatcher().dispatchOverheadMessage(
                this, message, color, easing);
    }
    
    
    /*
    public void setProperty(Object key, Property value)
    {
        if (_properties == null) {
            _properties = new HashMap();
        }
        _properties.put(key, value);
    }

    public boolean hasProperty(Object key)
    {
        return _properties == null ? false : _properties.containsKey(key);
    }
    
    public Property getProperty(Object key)
    {
        return _properties == null ? null : (Property)_properties.get(key); 
    }
    */

    public String toString()
    {
        return getName() + "@" + getLocation();
    }

    



}
